<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avatar_box">
        <img class="png" src="../assets/2.jpg" alt />
      </div>
      <!-- 
            登录表单区域
      -->
      <el-form
        ref="loginformref"
        label-width="0px"
        class="loginform"

      >
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input prefix-icon="iconfont icon-user" v-model="username" placeholder="用户名"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            prefix-icon="iconfont icon-3702mima"
            v-model="password"
            type="password"
            placeholder="密码"
          ></el-input>
        </el-form-item>
        <!-- 按钮区域 -->
        <el-form-item class="btns">
          <div class="forget">
              <router-link to="/forget" style="">忘记密码？</router-link>
          </div>
          
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="register">注册</el-button>
        </el-form-item>

         
      </el-form>
      
    </div>
    <AlertTip :alertText="alertText" v-show="showAlert" @closeTip="closeTip"/>
  </div>
</template>

<script>

import {mapActions,mapState} from 'vuex'
import AlertTip from '../components/AlertTip'
import {reqPwdLogin} from '../api'

export default {
  data() {
    return {
      //登录表单的数据对象
      username: "",
      password: "",

      showAlert:false,
      alertText: null,
      
      //表单的验证规则
     /* loginformrules: {
        username: [
          { required: true, message: "请输入名称", trigger: "blur" },
          { min: 3, max: 7, message: "长度在 3 到 7 个字符", trigger: "blur" },
        ],

        password: [
          { required: true, message: "请输入名称", trigger: "blur" },
          {
            min: 6,
            max: 15,
            message: "长度在 6 到 15 个字符",
            trigger: "blur",
          },
        ],
      },*/
    };
  },
  components:{
          AlertTip
    },
  computed:{
      ...mapState(['userinfo'])
    },
  methods: {
    ...mapActions([
      'recordUserInfo',
    ]),
    register() {
      // console.log(this)
      this.$router.push("/register");
    },
    async login() {  
      console.log(this.username)
      console.log(this.password)

       const {username,password}=this

       if ((!this.username)) {
        console.log(11111)
        this.alertOut('请输入用户名')
        return
      } else if (!this.password) {
        this.alertOut('请输入密码')
        return
      }

      const result= await reqPwdLogin(username,password)
      console.log(result)
      if (result.error_code === 0) {
        //成功
        console.log('login测试1：')
        console.log(result)
        //存储用户
        this.recordUserInfo(result)
        //跳转页面
        console.log(this.userinfo)
        this.$router.replace('/home')
      } else {
        //失败
        this.alertOut('用户名或者密码不正确，请重新登录！')
        this.pwd = ''
        this.name = ''
          //这里是我改的
      }
    },
        
   
    alertOut(str){
      this.showAlert = true
      this.alertText = str
    },
    closeTip() {
      this.showAlert = false
      this.alertText=''
    }
  }
  
};
</script>

<style>
.login_container {
  height: 100%;
  background-color: rgb(148, 168, 142);
}
.login_box {
  width: 450px;
  height: 350px;
  background-color: rgb(228, 228, 230);
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.avatar_box {
  height: 130px;
  width: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 0 10px #ddd;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -40%);
  background-color: #fff;
}
.avatar_box .png {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background-color: #eee;
}
.loginform {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
.btns {
  display: flex;
  justify-content: flex-end;
}
.forget{
  float: left;
  margin-right:180px;
  margin-top:5px;
}
</style>
